//
//  PhotoController.m
//  ToolFramework
//
//  Created by Apple on 2017/12/29.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "PhotoController.h"

@implementation PhotoController{
    UIViewController *selfController;
    UIImage *_photoImage;
    

}

+ (PhotoController *)sharedPhotoWithController:(UIViewController *)controller
{
    static PhotoController *photoController = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        photoController = [[PhotoController alloc]init];
    });
  
    [photoController editActioninViewController:controller];
    return photoController;
}

#pragma mark - 调用相机以及相册
-(void)editActioninViewController:(UIViewController*)viewController{
    selfController = viewController;
    UIAlertController* actionAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [actionAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"相机", @"InfoPlist", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //拍照
        [self showCamara];
    }]];
    [actionAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"相册", @"InfoPlist", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //相册
        [self showPhotoDic];
    }]];
    [actionAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"取消", @"InfoPlist", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [selfController presentViewController:actionAlert animated:YES completion:nil];
}

-(void)showCamara{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePickerController.delegate = self;
    imagePickerController.navigationBar.barTintColor = kNavigationBarColor;
    imagePickerController.navigationBar.tintColor = kNavigationBarTitleColor;
    imagePickerController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kNavigationBarTitleColor};
//    [TKThemeConfig TKGetThemeConfigWith:imagePickerController];
    
    [selfController presentViewController:imagePickerController animated:YES completion:^{
    }];
}

-(void)showPhotoDic{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePickerController.delegate = self;
    imagePickerController.navigationBar.barTintColor = kNavigationBarColor;
    imagePickerController.navigationBar.tintColor = kNavigationBarTitleColor;
    imagePickerController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kNavigationBarTitleColor};
//    [TKThemeConfig TKGetThemeConfigWith:imagePickerController];
    [selfController presentViewController:imagePickerController animated:YES completion:^{
    }];
}


#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
    if (!image) {
        image = [info valueForKey:UIImagePickerControllerOriginalImage];
    }
    
    _photoImage = image;
    
    [selfController dismissViewControllerAnimated:YES completion:^{
        if (self.blockImageDone) {
            self.blockImageDone(_photoImage);
        }
//        [self editPhotoByLeanCloud];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [selfController dismissViewControllerAnimated:YES completion:^{
        
    }];
}




@end
